package com.map.service.impl;

import com.map.dto.TuCengDto;
import com.map.entity.*;
import com.map.repository.MapTuCengDetailsRepository;
import com.map.repository.MapTuCengPageRepository;
import com.map.repository.MapTuCengStyleRepository;
import com.map.service.TucengService;
import com.map.util.StringUtil;
import com.map.entity.Maptuceng;
import com.map.repository.MapTuCengRepository;
import org.springframework.cache.support.NullValue;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

@Service
public class TucengServiceImpl implements TucengService {

    @Resource
    private MapTuCengRepository mapTuCengRepository;
    @Resource
    private MapTuCengDetailsRepository mapTuCengDetailsRepository;

    @Resource
    private MapTuCengPageRepository mapTuCengPageRepository;

    @Resource
    private MapTuCengStyleRepository mapTuCengStyleRepository;


    @Override
    public Page<Maptuceng> queryMaptuceng(TuCengDto tuCengDto, Integer pageIndex, Integer pageSize, Sort.Direction direction, String... properties) {
        Pageable pageable = new PageRequest(pageIndex, pageSize, direction, properties);
        Page<Maptuceng> all = mapTuCengRepository.findAll(new Specification<Maptuceng>() {
            @Override
            public Predicate toPredicate(Root<Maptuceng> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                Predicate predicate = cb.conjunction();
                if (!StringUtil.isEmpty(tuCengDto.getName())) {
                    predicate.getExpressions().add(cb.like(root.get("name"), "%" + tuCengDto.getName() + "%"));
                }
                if (!StringUtil.isEmpty(tuCengDto.getCode())) {
                    predicate.getExpressions().add(cb.like(root.get("code"), "%" + tuCengDto.getCode() + "%"));
                }
                if (!StringUtil.isEmpty(tuCengDto.getLinecolor())) {
                    predicate.getExpressions().add(cb.equal(root.get("linecolor"), tuCengDto.getLinecolor()));
                }
                if (!StringUtil.isEmpty(tuCengDto.getFillcolor())) {
                    predicate.getExpressions().add(cb.like(root.get("fillcolor"), "%" + tuCengDto.getFillcolor() + "%"));
                }

                if (!StringUtil.isEmpty(tuCengDto.getType())) {
                    //LikeName 查询时走type
                    predicate.getExpressions().add(cb.and(cb.notEqual(root.get("type"), "root"), cb.notEqual(root.get("type"), "category")));

                } else if (!StringUtil.isEmpty(tuCengDto.getId()) || !StringUtil.isEmpty(tuCengDto.getIds())) {
                    //ids查询
                    if (!StringUtil.isEmpty(tuCengDto.getIds())) {
                        CriteriaBuilder.In<Object> cbIn = cb.in(root.get("id"));
                        for (int i = 0; i < tuCengDto.getIds().split(",").length; i++) {
                            //存入值
                            cbIn.value(tuCengDto.getIds().split(",")[i]);
                        }
                        predicate.getExpressions().add(cb.and(cbIn));

                    } else {
                        predicate.getExpressions().add(cb.equal(root.get("id"), tuCengDto.getId()));
                    }
                } else {
                    if (!StringUtil.isEmpty(tuCengDto.getPageId())) {
                        predicate.getExpressions().add(cb.equal(root.get("pageId"), tuCengDto.getPageId()));
                    } else {
                        predicate.getExpressions().add(cb.or(cb.equal(root.get("pageId"), "null"), cb.isNull(root.get("pageId"))));
//                        predicate.getExpressions().add(cb.or(cb.and(predicate,cb.isNull(root.get("pageId")) ), cb.and(predicate,cb.isEmpty(root.get("pageId")))));

                    }
                }


                return predicate;
            }
        }, pageable);
        return all;
    }

    @Override
    public List<MapTucengDetails> queryMapTucengDetailsByLikeName(String name) {
//        return mapTuCengDetailsRepository.findByName(name);

        Pageable pageable = new PageRequest(0, 99999, Sort.Direction.DESC, "id");

        Page<MapTucengDetails> layername = mapTuCengDetailsRepository.findAll(new Specification<MapTucengDetails>() {
            @Override
            public Predicate toPredicate(Root<MapTucengDetails> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                Predicate predicate = cb.conjunction();
                if (!StringUtil.isEmpty(name)) {
                    predicate.getExpressions().add(cb.like(root.get("name"), "%" + name + "%"));
                }
                return predicate;
            }
        }, pageable);
        return layername.getContent();

    }

    @Override
    public void saveMaptuceng(Maptuceng maptuceng) {
        mapTuCengRepository.save(maptuceng);
    }

    @Override
    public void saveMapTucengDetails(MapTucengDetails mapTucengDetails) {
        mapTuCengDetailsRepository.save(mapTucengDetails);
    }


    @Override
    public void saveMapTucengPage(MapTucengPage mapTucengDetails) {
        mapTuCengPageRepository.save(mapTucengDetails);
    }

    @Override
    public void saveMaptucengStyle(MaptucengStyle maptucengStyle) {
        mapTuCengStyleRepository.save(maptucengStyle);
    }


    @Override
    public Page<MaptucengStyle> queryMapTuCengStyleRepository(String layername, Integer pageIndex, Integer pageSize, Sort.Direction direction, String... properties) {
        Pageable pageable = new PageRequest(pageIndex, pageSize, direction, properties);

        Page<MaptucengStyle> layername1 = mapTuCengStyleRepository.findAll(new Specification<MaptucengStyle>() {
            @Override
            public Predicate toPredicate(Root<MaptucengStyle> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                Predicate predicate = cb.conjunction();
                if (!StringUtil.isEmpty(layername)) {
                    predicate.getExpressions().add(cb.equal(root.get("layername"), layername));
                }
                return predicate;
            }
        }, pageable);
        return layername1;
    }
}
